package com.petalsgroup.petals.web.servlet.animation;

import com.petalsgroup.petals.pojo.CategoryDO;
import com.petalsgroup.petals.pojo.ResultInfo;
import com.petalsgroup.petals.service.animation.DirectoryService;
import com.petalsgroup.petals.service.animation.impl.DirectoryServiceImpl;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@WebServlet("/directory/*")
public class DirectoryServlet extends BaseServlet {
    private DirectoryService directoryService = new DirectoryServiceImpl();

    /**
     * 初始化分类头部信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void init(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResultInfo info = new ResultInfo();
        List<CategoryDO> list = directoryService.listCategory();
        info.setData(list);
        writeValue(resp, info);
    }

    /**
     * 初始化分类年份信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void initYear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResultInfo info = new ResultInfo();
        List<Integer> list = directoryService.listYear();
        // Collections.sort(list, (o1,o2)-> o2 - o1);
        info.setData(list);
        writeValue(resp, info);
    }
}
